import { queryInstOrderInfo } from '../../services/project'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentItem:{},
    reportList:[],
    defaultPictureUrl:'../../assets/inspection/photo.png',
    token: "",
    currentJson:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(!options) return

    const orderId = options.orderId
    this.getUserOrderList(orderId)
    this.setData({
      token: wx.getStorageSync('token')
    })
  },

  onShow(){
    this.setData({
      token: wx.getStorageSync('token')
    })
  },

  async getUserOrderList(orderId){
    let orderRes = await queryInstOrderInfo({orderId})
    if(orderRes.code === 200){
      let reportList = orderRes.rows
      // let reportList = []
      // let currentJson = currentItem.itemJson
      // if(currentItem.itemJson){
      //   reportList = JSON.parse(currentItem.itemJson)
      // }
      this.dealReportList(reportList)
      
      // this.setData({
      //   reportList
      // })     
    }
  },

  dealReportList(reportList){
    reportList.forEach(item => {
      item.moduleData = this.dealJson(JSON.parse(item.itemJson))
    })
    console.log("reportList>>>", reportList)
    this.setData({
      reportList
    })
  },

  dealJson(jsonList){
    let dataMap = {}
    jsonList.forEach(item => {
      if(item.isCollection == 'yes'){
        if(!dataMap[item.moduleName]){
          dataMap[item.moduleName] = []
          dataMap[item.moduleName].push(item)
        }else {
          dataMap[item.moduleName].push(item)
        }
      }
    })
    let moduleData = []
    for(let key in dataMap){
      moduleData.push({
        moduleName: key,
        moduleList: dataMap[key]
      })
    }
    return moduleData
  },

})